import java.util.*;

public class Main {

    public static HashMap<String,Integer> hash;
    public static String[] heap;
    public static int cnt;
    public static boolean cmp(String s1, int k1, String s2, int k2) {
        if (k1 != k2) return k1 > k2;
        return s1.compareTo(s2) < 0;
    }

    public static void shiftUp(String s, int val) {
        int parent = (cnt - 1) / 2;
        int child = cnt;
        while (parent >= 0) {
            if (cmp(heap[parent], hash.get(heap[parent]), heap[child], hash.get(heap[child]))) {
                String t = heap[parent];
                heap[parent] = heap[child];
                heap[child] = t;
                child = parent;
                parent = (child - 1) / 2;
            } else {
                break;
            }
        }
    }
    public static void shiftDown() {
        heap[0] = heap[cnt - 1];
        int parent = 0;
        int child = 2 * parent + 1;
        while (child < cnt) {
            if (cmp(heap[child], hash.get(heap[child]), heap[parent], hash.get(heap[parent]))) {
                String t = heap[parent];
                heap[parent] = heap[child];
                heap[child] = t;
                parent = child;
                child = parent * 2 + 1;
            } else {
                break;
            }
        }
    }
    public static void add(String s, int val) {
        if (cnt == 0) {
            heap[cnt++] = s;
            return;
        }
        heap[cnt] = s;
        shiftUp(s,val);
        cnt++;
    }
    public static void delete() {
        shiftDown();
        cnt--;
    }
    public static void createHeap(HashMap<String,Integer> hash, int k, List<String> list) {
        int i = 0;
        for (Map.Entry<String,Integer> entry : hash.entrySet()) {
            if (i < k) {
                add(entry.getKey(), entry.getValue());
            } else {
                if (cmp(entry.getKey(), entry.getValue(), heap[cnt - 1], hash.get(heap[cnt - 1]))) {
                    delete();
                    add(entry.getKey(), entry.getValue());
                }
            }
            i++;
        }

        for (i = 0; i < k; i++) {
            list.add(heap[i]);
        }

        Collections.sort(list, new Comparator<String>() {
            @Override
            public int compare(String s1, String s2) {
                int k1 = hash.get(s1);
                int k2 = hash.get(s2);
                if (k1 != k2) return k1 < k2 ? 1 : -1;
                return s2.compareTo(s1);
            }
        });
    }

    public static List<String> topKFrequent(String[] words, int k) {
        heap = new String[k + 1];
        List<String> list = new ArrayList<>();
        hash = new HashMap<>();
        for (int i = 0; i < words.length; i++) {
            if (hash.get(words[i]) == null) {
                hash.put(words[i],1);
            } else {
                int val = hash.get(words[i]);
                hash.put(words[i],val + 1);
            }
        }
        createHeap(hash, k, list);
        return list;
    }

    public static void main(String[] args) {
        String[] strings = {"the","day","is","sunny","the","the","the","sunny","is","is"};
        int k = 4;
        List<String> list = topKFrequent(strings, k);
        for (String s: list) {
            System.out.print(s + " ");
        }
    }
    public static void main1(String[] args) {
        Scanner sc = new Scanner(System.in);
        String s = sc.nextLine();
        String t = sc.nextLine();
        s = s.toUpperCase();
        t = t.toUpperCase();
        s.compareTo(t);
        HashMap<Character,Integer> hash = new HashMap<>();
        for (int i = 0; i < t.length(); i++) {
            char ch = t.charAt(i);
            if (hash.get(ch) == null) {
                hash.put(ch,1);
            } else {
                int val = hash.get(ch);
                hash.put(ch,val+1);
            }
        }
        for (int i = 0; i < s.length(); i++) {
            char ch = s.charAt(i);
            if (hash.get(ch) == null) {
/*                boolean flag = Character.isLetter(ch);
                if (flag && Character.isLowerCase(ch)) continue;*/
                System.out.print(ch);
                hash.put(ch,1);
            }
        }
    }
}